﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using FormControllers;
using Ecopsy.DataTier;
using Ecopsy.Tools;

public partial class iFarmeDictionary_ConstractionType : System.Web.UI.Page
{
    #region Private

    private wsDictionary _ws_Dictionary = new wsDictionary ();

    private string TreeDictionaryID { get { return Request.QueryString["TreeDictionaryID"]; } }

    private string FolderID { get { return Request.QueryString["FolderID"]; } }


    protected bool IsSystemDictionry
    {
        get { return Dictionary.Get_TreeDictionaryTypeList_Item().IsSystem > 0; } 
    }

    private TreeDictionaryItem   Dictionary
    {
        get
        {
            
            // получить справочник 
            TreeDictionaryItem tree = (TreeDictionaryItem)_ws_Dictionary.GetTreeDictionary(FolderID, TreeDictionaryID);
            
            if (null == tree) tree = _ws_Dictionary.CreateTreeDictionary(FolderID, TreeDictionaryID, "Новый справочник");
            return tree;
        }
    }

    /// <summary>
    /// вернуть требуемый вид справочника
    /// </summary>
    /// <returns></returns>
    private F_TreeDataSource GetTreeSource()
    {        
            
        // определить делегат поставляющий класс определяющий формат ноды в дереве
        Func<TreeEntitySingle, F_TreeDataSource.F_TreeDataSourceView.F_TreeHierarchyData> formatter = delegate(TreeEntitySingle _tr)
        {
            return (F_TreeDataSource.F_TreeDataSourceView.F_TreeHierarchyData)new ТрееDictionaryItemFormatter(_tr);
        };

        // вернуть древовидныйисточник данных
        return  new F_TreeDataSource(Dictionary, formatter);

    }
   
    
    #endregion
    
    #region Protected
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            MyBind();
        }
    }
    
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

    }
   
    protected string SelectedText 
    {
        get {
                if (null == tvDictionary.SelectedNode) return string.Empty;
                return tvDictionary.SelectedNode.Text;
  
        }
    }

    protected string SelectedValue
    {
        get
        {
            if (null == tvDictionary.SelectedNode) return string.Empty;
            return tvDictionary.SelectedNode.DataPath;

        }
    }

    /// <summary>
    /// Не трогать если  0 уровень, если не выделен parent
    /// </summary>
    protected bool EnableDel 
    {
        get 
        {
            if (null == tvDictionary.SelectedNode) return false ;
            if (0 == tvDictionary.SelectedNode.Depth) return false;
            return true;
        }
    }

    protected bool EnableAdd
    {
        get
        {
            if (null == tvDictionary.SelectedNode) return false;            
            return true;
        }
    }
   
    private void MyBind()
    {
        tvDictionary.DataSource = GetTreeSource();
        DataBind();

    }

    protected void tvDictionary_SelectedNodeChanged1(object sender, EventArgs e)
    {
        DataBind();
    }
       
    /// <summary>
    /// Если есть выделенная ветка - считать в нее значение txtNodeVal.Text
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if(SelectedValue.Equals (string.Empty ))  return ;
        _ws_Dictionary.UpdateTreeDictionaryBranch(FolderID, TreeDictionaryID, SelectedValue, txtNodeVal.Text);
        MyBind ();
        wsAgency ws = new wsAgency();
        
    }
    
    protected void AddButton_Click(object sender, EventArgs e)
    {
        if (SelectedValue.Equals(string.Empty)) return;
        _ws_Dictionary.AddTreeDictionaryBranch(FolderID, TreeDictionaryID, SelectedValue, "Новое значение");
        MyBind();
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (SelectedValue.Equals(string.Empty)) return;
        _ws_Dictionary.RemoveTreeDictionaryBranch(FolderID, TreeDictionaryID, SelectedValue);
        MyBind();

    }
#endregion

    

}
